﻿using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;

namespace Scrabble.Behaviors
{
    public static class Native
    {
        [StructLayout(LayoutKind.Sequential)]
        private struct POINT
        {
            public Int32 X;
            public Int32 Y;
        };

        [DllImport("user32.dll")]
        private static extern bool GetCursorPos(ref POINT pt);

        [DllImport("user32.dll")]
        private static extern bool ScreenToClient(IntPtr hwnd, ref POINT pt);

        public static Point CursorPositionOnScreen()
        {
            var cursorPosition = new POINT();
            GetCursorPos(ref cursorPosition);

            return new Point(cursorPosition.X, cursorPosition.Y);
        }

        public static Point CursorPosition(this Visual visual)
        {
            var cursorPosition = new POINT();
            GetCursorPos(ref cursorPosition);

            return visual.PointFromScreen(new Point(cursorPosition.X, cursorPosition.Y));
        }
    }
}